package edu.uwm.cs552;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import java.util.logging.Logger;
import edu.uwm.cs552.util.JLoggerList;



/**
 * Main frame for the hexagonal board editor.
 * By default hidden geography is visible.
 */
public class PlayerFrame extends JFrame {

	/**
	 * Keep Eclipse happy
	 */
	private static final long serialVersionUID = 1L;
	private HexBoardPanel hexBoardPanel;
	private JPanel topPanel;
	private HexBoard hexBoard;
	private JLoggerList logger;
	private JComboBox editActionComboBox;
	private Player player;
	private RailGame railGame;
	private HexCoordinate previous;

	private static final int DEFAULT_WIDTH = 75;
	private static final int DEFAULT_HEIGHT = 50;
	
	private HexBoardEditAction defaultEditAction;
	private HexBoardEditAction[] editActions;

	public PlayerFrame(RailGame game, Player p, HexBoard board) {
		super(p == null ? "Game Board" : "Player " + p.getPlayerId());
		
		hexBoard = board;
		player = p;
		railGame = game;
		
		defaultEditAction = new MoveDraw(HexCoordinate.ORIGIN, player);
		editActions = new HexBoardEditAction[]{
			defaultEditAction, new RailDraw(RailState.PROPOSED, player),
			new MoveDraw(null, player), new RailDraw(null, player)
		};
		
		getContentPane().setLayout(new BorderLayout());
		hexBoardPanel = new HexBoardPanel(hexBoard);
		if (player == null)
			railGame.addObserver(hexBoardPanel);
		
		getContentPane().add(new JScrollPane(hexBoardPanel));
		topPanel = new JPanel();
		topPanel.add(new ScaleComboBox(hexBoardPanel));
		
		if (p != null)
			setupPlayerUI();
		
		JPanel loggerPanel = new JPanel();
		
		logger = new JLoggerList(Logger.getLogger("Game" + (p == null ? "" : ".Player" + p.getPlayerId())));
		loggerPanel.add(logger);
		JScrollPane logs = new JScrollPane(loggerPanel);
		logs.setPreferredSize(new Dimension(500, 75));
		
		getContentPane().add(topPanel,BorderLayout.NORTH);
		getContentPane().add(logs,BorderLayout.SOUTH);
				

		
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new WindowAdapter(){
			@Override
			public void windowClosing(WindowEvent we) {
				doQuit();
			}
		});
	}
	
	private void setupPlayerUI() {
		player.addObserver(hexBoardPanel);
		
		MouseAdapter mouseActions = new MouseAdapter(){
			@Override
			public void mouseDragged(MouseEvent arg0) {
				editMap(arg0.getPoint());
			}
			@Override
			public void mousePressed(MouseEvent arg0) {
				editMap(arg0.getPoint());
			}
		};
		
		hexBoardPanel.addMouseListener(mouseActions);
		hexBoardPanel.addMouseMotionListener(mouseActions);
		
		final JButton moveBtn = new JButton("Move");
		moveBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(player.getMoveDisabled())
					moveBtn.disable();
				else {
					moveBtn.enable();
					player.move();
				}
			}
			
		});
		topPanel.add(moveBtn);
		
		
		editActionComboBox = new JComboBox(editActions);
		editActionComboBox.setRenderer(new HexBoardEditActionRenderer());
		topPanel.add(editActionComboBox);
		editActionComboBox.setSelectedItem(defaultEditAction);
		editActionComboBox.setMaximumRowCount(editActions.length);
		
		JButton clearLogBtn = new JButton("Clear Log");
		clearLogBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				logger.clear();
			}
			
		});
		topPanel.add(clearLogBtn);
		JLabel moneyLable = new JLabel("Money: ");
		JTextField money = new JTextField("$ " + player.getMoney());
		topPanel.add(moneyLable);
		topPanel.add(money);
	}
	

	private void editMap(Point p) {
		final double scale = hexBoardPanel.getScale();
		final double bound = hexBoardPanel.getBound();
		HexCoordinate h = HexCoordinate.fromPoint(p, scale);
		if(!h.equals(previous)) {
			((HexBoardEditAction)(editActionComboBox.getSelectedItem())).apply(hexBoard, p, scale, bound);
			previous = h;
		}
	}
	
	
	private void doQuit() {
		if (JOptionPane.showConfirmDialog(this, "Really Quit?", "Confirm Exit", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
			System.exit(0);
		}
	}
}
